home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / textfile / faqs / unix_faq / faq / part3 < prev    next >
Encoding:
Internet Message Format  |  1992-12-27  |  27.9 KB

  1. Xref: bloom-picayune.mit.edu comp.unix.questions:51335 comp.unix.shell:8341 news.answers:4777
  2. Path: bloom-picayune.mit.edu!senator-bedfellow.mit.edu!senator-bedfellow.mit.edu!usenet
  3. From: tmatimar@empress.com (Ted M A Timar)
  4. Newsgroups: comp.unix.questions,comp.unix.shell,news.answers
  5. Subject: Unix - Frequently Asked Questions (3/7) [Frequent posting]
  6. Supersedes: <unix-faq/faq/part3_723967331@athena.mit.edu>
  7. Followup-To: comp.unix.questions
  8. Date: 24 Dec 1992 06:03:13 GMT
  9. Organization: Empress Software
  10. Lines: 636
  11. Approved: news-answers-request@MIT.Edu
  12. Distribution: world
  13. Expires: 21 Jan 1993 06:02:09 GMT
  14. Message-ID: <unix-faq/faq/part3_725176929@athena.mit.edu>
  15. References: <unix-faq/faq/contents_725176929@athena.mit.edu>
  16. NNTP-Posting-Host: pit-manager.mit.edu
  17. X-Last-Updated: 1992/12/09
  18.  
  19. Archive-name: unix-faq/faq/part3
  20. Version: $Id: part3,v 2.1 92/12/04 07:43:49 tmatimar Exp $
  21.  
  22. These seven articles contain the answers to some Frequently Asked
  23. Questions often seen in comp.unix.questions and comp.unix.shell.
  24. Please don't ask these questions again, they've been answered plenty
  25. of times already - and please don't flame someone just because they may
  26. not have read this particular posting.  Thank you.
  27.  
  28. These articles are divided approximately as follows:
  29.  
  30.       1.*) General questions.
  31.       2.*) Relatively basic questions, likely to be asked by beginners.
  32.       3.*) Intermediate questions.
  33.       4.*) Advanced questions, likely to be asked by people who thought
  34.        they already knew all of the answers.
  35.       5.*) Questions pertaining to the various shells, and the differences.
  36.       6.*) An overview of Unix variants.
  37.       7.*) An comparison of configuration management systems (RCS, SCCS).
  38.  
  39. This article includes answers to:
  40.  
  41.       3.1)  How do I find out the creation time of a file?
  42.       3.2)  How do I use "rsh" without having the rsh hang around
  43.               until the remote command has completed?
  44.       3.3)  How do I truncate a file?
  45.       3.4)  Why doesn't find's "{}" symbol do what I want?
  46.       3.5)  How do I set the permissions on a symbolic link?
  47.       3.6)  How do I "undelete" a file?
  48.       3.7)  How can a process detect if it's running in the background?
  49.       3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  50.       3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  51.               programs from a shell script or in the background?
  52.       3.10) How do I find out the process ID of a program with a particular
  53.             name from inside a shell script or C program?
  54.       3.11) How do I check the exit status of a remote command
  55.             executed via "rsh" ?
  56.       3.12) Is it possible to pass shell variable settings into an awk program?
  57.       3.13) How do I get rid of zombie processes that persevere?
  58.       3.14) How do I get lines from a pipe as they are written instead of
  59.             only in larger blocks.
  60.  
  61. If you're looking for the answer to, say, question 3.5, and want to skip
  62. everything else, you can search ahead for the regular expression "^3.5)".
  63.  
  64. While these are all legitimate questions, they seem to crop up in
  65. comp.unix.questions or comp.unix.shell on an annual basis, usually
  66. followed by plenty of replies (only some of which are correct) and then
  67. a period of griping about how the same questions keep coming up.  You
  68. may also like to read the monthly article "Answers to Frequently Asked
  69. Questions" in the newsgroup "news.announce.newusers", which will tell
  70. you what "UNIX" stands for.
  71.  
  72. With the variety of Unix systems in the world, it's hard to guarantee
  73. that these answers will work everywhere.  Read your local manual pages
  74. before trying anything suggested here.  If you have suggestions or
  75. corrections for any of these answers, please send them to to
  76. tmatimar@empress.com.
  77.  
  78. 3.1)  How do I find out the creation time of a file?
  79.  
  80.       You can't - it isn't stored anywhere.  Files have a last-modified
  81.       time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
  82.       and an inode change time (shown by "ls -lc"). The latter is often
  83.       referred to as the "creation time" - even in some man pages -
  84.       but that's wrong; it's also set by such operations as mv, ln,
  85.       chmod, chown and chgrp.
  86.  
  87.       The man page for "stat(2)" discusses this.
  88.  
  89. 3.2)  How do I use "rsh" without having the rsh hang around until the
  90.       remote command has completed?
  91.  
  92.       (See note in question 2.7 about what "rsh" we're talking about.)
  93.  
  94.       The obvious answers fail:
  95.               rsh machine command &
  96.       or      rsh machine 'command &'
  97.  
  98.       For instance, try doing   rsh machine 'sleep 60 &' and you'll see
  99.       that the 'rsh' won't exit right away.  It will wait 60 seconds
  100.       until the remote 'sleep' command finishes, even though that
  101.       command was started in the background on the remote machine.  So
  102.       how do you get the 'rsh' to exit immediately after the 'sleep' is
  103.       started?
  104.  
  105.       The solution - if you use csh on the remote machine:
  106.  
  107.         rsh machine -n 'command >&/dev/null </dev/null &'
  108.  
  109.       If you use sh on the remote machine:
  110.  
  111.         rsh machine -n 'command >/dev/null 2>&1 </dev/null &'
  112.  
  113.       Why?  "-n" attaches rsh's stdin to /dev/null so you could run the
  114.       complete rsh command in the background on the LOCAL machine.
  115.       Thus "-n" is equivalent to another specific "< /dev/null".
  116.       Furthermore, the input/output redirections on the REMOTE machine
  117.       (inside the single quotes) ensure that rsh thinks the session can
  118.       be terminated (there's no data flow any more.)
  119.  
  120.       Note: The file that you redirect to/from on the remote machine
  121.       doesn't have to be /dev/null; any ordinary file will do.
  122.  
  123.       In many cases, various parts of these complicated commands
  124.       aren't necessary.
  125.  
  126. 3.3)  How do I truncate a file?
  127.  
  128.       The BSD function ftruncate() sets the length of a file.  Xenix -
  129.       and therefore SysV r3.2 and later - has the chsize() system
  130.       call.  For other systems, the only kind of truncation you can do
  131.       is truncation to length zero with creat() or open(..., O_TRUNC).
  132.  
  133. 3.4)  Why doesn't find's "{}" symbol do what I want?
  134.  
  135.       "find" has a -exec option that will execute a particular command
  136.       on all the selected files. Find will replace any "{}" it sees
  137.       with the name of the file currently under consideration.
  138.  
  139.       So, some day you might try to use "find" to run a command on
  140.       every file, one directory at a time.  You might try this:
  141.  
  142.     find /path -type d -exec command {}/\* \;
  143.  
  144.       hoping that find will execute, in turn
  145.  
  146.     command directory1/*
  147.     command directory2/*
  148.     ...
  149.  
  150.       Unfortunately, find only expands the "{}" token when it appears
  151.       by itself.  Find will leave anything else like "{}/*" alone, so
  152.       instead of doing what you want, it will do
  153.  
  154.     command {}/*
  155.     command {}/*
  156.     ...
  157.  
  158.       once for each directory.  This might be a bug, it might be a
  159.       feature, but we're stuck with the current behaviour.
  160.  
  161.       So how do you get around this?  One way would be to write a
  162.       trivial little shell script, let's say "./doit", that consists of
  163.  
  164.     command "$1"/*
  165.  
  166.       You could then use
  167.  
  168.     find /path -type d -exec ./doit {} \;
  169.  
  170.       Or if you want to avoid the "./doit" shell script, you can use
  171.  
  172.     find /path -type d -exec sh -c 'command $0/*' {} \;
  173.  
  174.       (This works because within the 'command' of "sh -c 'command' A B C ...",
  175.        $0 expands to A, $1 to B, and so on.)
  176.  
  177.       or you can use the construct-a-command-with-sed trick
  178.  
  179.     find /path -type d -print | sed 's:.*:command &/*:' | sh
  180.  
  181.       If all you're trying to do is cut down on the number of times
  182.       that "command" is executed, you should see if your system has the
  183.       "xargs" command.  Xargs reads arguments one line at a time from
  184.       the standard input and assembles as many of them as will fit into
  185.       one command line.  You could use
  186.  
  187.     find /path -print | xargs command
  188.  
  189.       which would result in one or more executions of
  190.  
  191.     command file1 file2 file3 file4 dir1/file1 dir1/file2
  192.  
  193.       Unfortunately this is not a perfectly robust or secure solution.
  194.       Xargs expects its input lines to be terminated with newlines, so
  195.       it will be confused by files with odd characters such as newlines
  196.       in their names.
  197.  
  198. 3.5)  How do I set the permissions on a symbolic link?
  199.  
  200.       Permissions on a symbolic link don't really mean anything.  The
  201.       only permissions that count are the permissions on the file that
  202.       the link points to.
  203.  
  204. 3.6)  How do I "undelete" a file?
  205.  
  206.       Someday, you are going to accidentally type something like
  207.       "rm * .foo", and find you just deleted "*" instead of "*.foo".
  208.       Consider it a rite of passage.
  209.  
  210.       Of course, any decent systems administrator should be doing
  211.       regular backups.  Check with your sysadmin to see if a recent
  212.       backup copy of your file is available.  But if it isn't, read
  213.       on.
  214.  
  215.       For all intents and purposes, when you delete a file with "rm" it
  216.       is gone.  Once you "rm" a file, the system totally forgets which
  217.       blocks scattered around the disk comprised your file.  Even
  218.       worse, the blocks from the file you just deleted are going to be
  219.       the first ones taken and scribbled upon when the system needs
  220.       more disk space.  However, never say never.  It is theoretically
  221.       possible *if* you shut down the system immediately after the "rm"
  222.       to recover portions of the data.  However, you had better have a
  223.       very wizardly type person at hand with hours or days to spare to
  224.       get it all back.
  225.  
  226.       Your first reaction when you "rm" a file by mistake is why not
  227.       make a shell alias or procedure which changes "rm" to move files
  228.       into a trash bin rather than delete them?  That way you can
  229.       recover them if you make a mistake, and periodically clean out
  230.       your trash bin.  Two points:  first, this is generally accepted
  231.       as a *bad* idea.  You will become dependent upon this behaviour
  232.       of "rm", and you will find yourself someday on a normal system
  233.       where "rm" is really "rm", and you will get yourself in trouble.
  234.       Second, you will eventually find that the hassle of dealing with
  235.       the disk space and time involved in maintaining the trash bin, it
  236.       might be easier just to be a bit more careful with "rm".  For
  237.       starters, you should look up the "-i" option to "rm" in your
  238.       manual.
  239.  
  240.       If you are still undaunted, then here is a possible simple
  241.       answer.  You can create yourself a "can" command which moves
  242.       files into a trashcan directory. In csh(1) you can place the
  243.       following commands in the ".login" file in your home directory:
  244.  
  245.     alias can    'mv \!* ~/.trashcan'       # junk file(s) to trashcan
  246.     alias mtcan    'rm -f ~/.trashcan/*'       # irretrievably empty trash
  247.     if ( ! -d ~/.trashcan ) mkdir ~/.trashcan  # ensure trashcan exists
  248.  
  249.       You might also want to put a:
  250.  
  251.     rm -f ~/.trashcan/*
  252.  
  253.       in the ".logout" file in your home directory to automatically
  254.       empty the trash when you log out.  (sh and ksh versions are left
  255.       as an exercise for the reader.)
  256.  
  257.       MIT's Project Athena has produced a comprehensive
  258.       delete/undelete/expunge/purge package, which can serve as a
  259.       complete replacement for rm which allows file recovery.  This
  260.       package was posted to comp.sources.misc (volume 17, issue
  261.       023-026)
  262.  
  263. 3.7)  How can a process detect if it's running in the background?
  264.  
  265.       First of all: do you want to know if you're running in the
  266.       background, or if you're running interactively? If you're
  267.       deciding whether or not you should print prompts and the like,
  268.       that's probably a better criterion. Check if standard input
  269.       is a terminal:
  270.  
  271.         sh: if [ -t 0 ]; then ... fi
  272.         C: if(isatty(0)) { ... }
  273.  
  274.       In general, you can't tell if you're running in the background.
  275.       The fundamental problem is that different shells and different
  276.       versions of UNIX have different notions of what "foreground" and
  277.       "background" mean - and on the most common type of system with a
  278.       better-defined notion of what they mean, programs can be moved
  279.       arbitrarily between foreground and background!
  280.  
  281.       UNIX systems without job control typically put a process into the
  282.       background by ignoring SIGINT and SIGQUIT and redirecting the
  283.       standard input to "/dev/null"; this is done by the shell.
  284.  
  285.       Shells that support job control, on UNIX systems that support job
  286.       control, put a process into the background by giving it a process
  287.       group ID different from the process group to which the terminal
  288.       belongs.  They move it back into the foreground by setting the
  289.       terminal's process group ID to that of the process.  Shells that
  290.       do *not* support job control, on UNIX systems that support job
  291.       control, typically do what shells do on systems that don't
  292.       support job control.
  293.  
  294. 3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  295.  
  296.       Take the following example:
  297.  
  298.     foo=bar
  299.  
  300.     while read line
  301.     do
  302.         # do something with $line
  303.         foo=bletch
  304.     done < /etc/passwd
  305.  
  306.     echo "foo is now: $foo"
  307.  
  308.       Despite the assignment ``foo=bletch'' this will print
  309.       ``foo is now: bar'' in many implementations of the Bourne shell.
  310.       Why?  Because of the following, often undocumented, feature of
  311.       historic Bourne shells: redirecting a control structure (such as
  312.       a loop, or an ``if'' statement) causes a subshell to be created,
  313.       in which the structure is executed; variables set in that
  314.       subshell (like the ``foo=bletch'' assignment) don't affect the
  315.       current shell, of course.
  316.  
  317.       The POSIX 1003.2 Shell and Tools Interface standardization
  318.       committee forbids the behaviour described above, i.e. in P1003.2
  319.       conformant Bourne shells the example will print ``foo is now:
  320.       bletch''.
  321.  
  322.       In historic (and P1003.2 conformant) implementations you can use
  323.       the following `trick' to get around the redirection problem:
  324.  
  325.     foo=bar
  326.  
  327.     # make file descriptor 9 a duplicate of file descriptor 0 (stdin);
  328.     # then connect stdin to /etc/passwd; the original stdin is now
  329.     # `remembered' in file descriptor 9; see dup(2) and sh(1)
  330.     exec 9<&0 < /etc/passwd
  331.  
  332.     while read line
  333.     do
  334.         # do something with $line
  335.         foo=bletch
  336.     done
  337.  
  338.     # make stdin a duplicate of file descriptor 9, i.e. reconnect
  339.     # it to the original stdin; then close file descriptor 9
  340.     exec 0<&9 9<&-
  341.  
  342.     echo "foo is now: $foo"
  343.  
  344.       This should always print ``foo is now: bletch''.
  345.       Right, take the next example:
  346.  
  347.     foo=bar
  348.  
  349.     echo bletch | read foo
  350.  
  351.     echo "foo is now: $foo"
  352.  
  353.       This will print ``foo is now: bar'' in many implementations,
  354.       ``foo is now: bletch'' in some others.  Why?  Generally each part
  355.       of a pipeline is run in a different subshell; in some
  356.       implementations though, the last command in the pipeline is made
  357.       an exception: if it is a builtin command like ``read'', the
  358.       current shell will execute it, else another subshell is created.
  359.  
  360.       POSIX 1003.2 allows both behaviours so portable scripts cannot
  361.       depend on any of them.
  362.  
  363. 3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  364.       programs from a shell script or in the background?
  365.  
  366.       These programs expect a terminal interface.  Shells makes no
  367.       special provisions to provide one.  Hence, such programs cannot
  368.       be automated in shell scripts.
  369.  
  370.       The 'expect' program provides a programmable terminal interface
  371.       for automating interaction with such programs.  The following
  372.       expect script is an example of a non-interactive version of
  373.       passwd(1).
  374.  
  375.     # username is passed as 1st arg, password as 2nd
  376.     set password [index $argv 2]
  377.     spawn passwd [index $argv 1]
  378.     expect "*password:"
  379.     send "$password\r"
  380.     expect "*password:"
  381.     send "$password\r"
  382.     expect eof
  383.  
  384.       expect can partially automate interaction which is especially
  385.       useful for telnet, rlogin, debuggers or other programs that have
  386.       no built-in command language.  The distribution provides an
  387.       example script to rerun rogue until a good starting configuration
  388.       appears.  Then, control is given back to the user to enjoy the game.
  389.  
  390.       Fortunately some programs have been written to manage the
  391.       connection to a pseudo-tty so that you can run these sorts of
  392.       programs in a script.
  393.  
  394.       To get expect, email "send pub/expect/expect.shar.Z" to
  395.       library@cme.nist.gov or anonymous ftp same from
  396.       durer.cme.nist.gov.
  397.  
  398.       Another solution is provided by the pty 4.0 program, which runs a
  399.       program under a pseudo-tty session and was posted to
  400.       comp.sources.unix, volume 25.  A pty-based solution using named
  401.       pipes to do the same as the above might look like this:
  402.  
  403.     #!/bin/sh
  404.     /etc/mknod out.$$ p; exec 2>&1
  405.     ( exec 4<out.$$; rm -f out.$$
  406.     <&4 waitfor 'password:'
  407.         echo "$2"
  408.     <&4 waitfor 'password:'
  409.         echo "$2"
  410.     <&4 cat >/dev/null
  411.     ) | ( pty passwd "$1" >out.$$ )
  412.  
  413.       Here, 'waitfor' is a simple C program that searches for
  414.       its argument in the input, character by character.
  415.  
  416.       A simpler pty solution (which has the drawback of not
  417.       synchronizing properly with the passwd program) is
  418.  
  419.     #!/bin/sh
  420.     ( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"
  421.  
  422. 3.10) How do I find out the process ID of a program with a particular
  423.       name from inside a shell script or C program?
  424.  
  425.       In a shell script:
  426.  
  427.       There is no utility specifically designed to map between program
  428.       names and process IDs.  Furthermore, such mappings are often
  429.       unreliable, since it's possible for more than one process to have
  430.       the same name, and since it's possible for a process to change
  431.       its name once it starts running.  However, a pipeline like this
  432.       can often be used to get a list of processes (owned by you) with
  433.       a particular name:
  434.  
  435.         ps ux | awk '/name/ && !/awk/ {print $2}'
  436.  
  437.       You replace "name" with the name of the process for which you are
  438.       searching.
  439.  
  440.       The general idea is to parse the output of ps, using awk or grep
  441.       or other utilities, to search for the lines with the specified
  442.       name on them, and print the PID's for those lines.  Note that the
  443.       "!/awk/" above prevents the awk process for being listed.
  444.  
  445.       You may have to change the arguments to ps, depending on what
  446.       kind of Unix you are using.
  447.  
  448.       In a C program:
  449.  
  450.       Just as there is no utility specifically designed to map between
  451.       program names and process IDs, there are no (portable) C library
  452.       functions to do it either.
  453.  
  454.       However, some vendors provide functions for reading Kernel
  455.       memory; for example, Sun provides the "kvm_" functions, and Data
  456.       General provides the "dg_" functions.  It may be possible for any
  457.       user to use these, or they may only be useable by the super-user
  458.       (or a user in group "kmem") if read-access to kernel memory on
  459.       your system is restricted.  Furthermore, these functions are
  460.       often not documented or documented badly, and might change from
  461.       release to release.
  462.  
  463.       Some vendors provide a "/proc" filesystem, which appears as a
  464.       directory with a bunch of filenames in it.  Each filename is a
  465.       number, corresponding to a process ID, and you can open the file
  466.       and read it to get information about the process.  Once again,
  467.       access to this may be restricted, and the interface to it may
  468.       change from system to system.
  469.  
  470.       If you can't use vendor-specific library functions, and you
  471.       don't have /proc, and you still want to do this completely
  472.       in C, you
  473.       are going to have to do the grovelling through kernel memory
  474.       yourself.  For a good example of how to do this on many systems,
  475.       see the sources to "ofiles", available in the comp.sources.unix
  476.       archives.  (A package named "kstuff" to help with kernel
  477.       grovelling was posted to alt.sources in May 1991 and is also
  478.       available via anonymous ftp as
  479.       usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  480.       wuarchive.wustl.edu.)
  481.  
  482. 3.11) How do I check the exit status of a remote command
  483.       executed via "rsh" ?
  484.  
  485.       This doesn't work:
  486.  
  487.     rsh some-machine some-crummy-command || echo "Command failed"
  488.  
  489.       The exit status of 'rsh' is 0 (success) if the rsh program
  490.       itself completed successfully, which probably isn't what
  491.       you wanted.
  492.  
  493.       If you want to check on the exit status of the remote program,
  494.       you can try using Maarten Litmaath's 'ersh' script, which was
  495.       posted to alt.sources in January, 1991.  ersh is a shell script
  496.       that calls rsh, arranges for the remote machine to echo the
  497.       status of the command after it completes, and exits with that
  498.       status.
  499.  
  500. 3.12) Is it possible to pass shell variable settings into an awk program?
  501.  
  502.       There are two different ways to do this.  The first involves
  503.       simply expanding the variable where it is needed in the program.
  504.       For example, to get a list of all ttys you're using:
  505.  
  506.     who | awk '/^'"$USER"'/ { print $2 }'                (1)
  507.  
  508.       Single quotes are usually used to enclose awk programs because
  509.       the character '$' is often used in them, and '$' will be
  510.       interpreted by the shell if enclosed inside double quotes, but
  511.       not if enclosed inside single quotes.  In this case, we *want*
  512.       the '$' in "$USER" to be interpreted by the shell, so we close
  513.       the single quotes and then put the "$USER" inside double quotes.
  514.       Note that there are no spaces in any of that, so the shell will
  515.       see it all as one argument.  Note, further, that the double
  516.       quotes probably aren't necessary in this particular case (i.e. we
  517.       could have done
  518.  
  519.     who | awk '/^'$USER'/ { print $2 }'                (2)
  520.  
  521.       ), but they should be included nevertheless because they are
  522.       necessary when the shell variable in question contains special
  523.       characters or spaces.
  524.  
  525.       The second way to pass variable settings into awk is to use an
  526.       often undocumented feature of awk which allows variable settings
  527.       to be specified as "fake file names" on the command line.  For
  528.       example:
  529.  
  530.     who | awk '$1 == user { print $2 }' user="$USER" -        (3)
  531.  
  532.       Variable settings take effect when they are encountered on the
  533.       command line, so, for example, you could instruct awk on how to
  534.       behave for different files using this technique.  For example:
  535.  
  536.     awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  537.  
  538.       Note that some versions of awk will cause variable settings
  539.       encountered before any real filenames to take effect before the
  540.       BEGIN block is executed, but some won't so neither way should be
  541.       relied upon.
  542.  
  543.       Note, further, that when you specify a variable setting, awk
  544.       won't automatically read from stdin if no real files are
  545.       specified, so you need to add a "-" argument to the end of your
  546.       command, as I did at (3) above.
  547.  
  548. 3.13) How do I get rid of zombie processes that persevere?
  549.  
  550.       From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  551.       Date: Fri, 17 Jan 92 14:40:09 -0500
  552.  
  553.       Unfortunately, it's impossible to generalize how the death of
  554.       child processes should behave, because the exact mechanism varies
  555.       over the various flavors of Unix.
  556.  
  557.       First of all, by default, you have to do a wait() for child
  558.       processes under ALL flavors of Unix.  That is, there is no flavor
  559.       of Unix that I know of that will automatically flush child
  560.       processes that exit, even if you don't do anything to tell it to
  561.       do so.
  562.  
  563.       Second, under some SysV-derived systems, if you do
  564.       "signal(SIGCHLD, SIG_IGN)" (well, actually, it may be SIGCLD
  565.       instead of SIGCHLD, but most of the newer SysV systems have
  566.       "#define SIGCHLD SIGCLD" in the header files), then child
  567.       processes will be cleaned up automatically, with no further
  568.       effort in your part.  The best way to find out if it works at
  569.       your site is to try it, although if you are trying to write
  570.       portable code, it's a bad idea to rely on this in any case.
  571.       Unfortunately, POSIX doesn't allow you to do this; the behavior
  572.       of setting the SIGCHLD to SIG_IGN under POSIX is undefined, so
  573.       you can't do it if your program is supposed to be
  574.       POSIX-compliant.
  575.  
  576.       If you can't use SIG_IGN to force automatic clean-up, then you've
  577.       got to write a signal handler to do it.  It isn't easy at all to
  578.       write a signal handler that does things right on all flavors of
  579.       Unix, because of the following inconsistencies:
  580.  
  581.       On some flavors of Unix, the SIGCHLD signal handler is called if
  582.       one *or more* children have died.  This means that if your signal
  583.       handler only does one wait() call, then it won't clean up all of
  584.       the children.  Fortunately, I believe that all Unix flavors for
  585.       which this is the case have available to the programmer the
  586.       wait3() call, which allows the WNOHANG option to check whether or
  587.       not there are any children waiting to be cleaned up.  Therefore,
  588.       on any system that has wait3(), your signal handler should call
  589.       wait3() over and over again with the WNOHANG option until there
  590.       are no children left to clean up.
  591.  
  592.       On SysV-derived systems, SIGCHLD signals are regenerated if there
  593.       are child processes still waiting to be cleaned up after you exit
  594.       the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  595.       systems to assume when the signal handler gets called that you
  596.       only have to clean up one signal, and assume that the handler
  597.       will get called again if there are more to clean up after it
  598.       exits.
  599.  
  600.       On older systems, signal handlers are automatically reset to
  601.       SIG_DFL when the signal handler gets called.  On such systems,
  602.       you have to put "signal(SIGCHILD, catcher_func)" (where
  603.       "catcher_func" is the name of the handler function) as the first
  604.       thing in the signal handler, so that it gets reset.
  605.       Unfortunately, there is a race condition which may cause you to
  606.       get a SIGCHLD signal and have it ignored between the time your
  607.       handler gets called and the time you reset the signal.
  608.       Fortunately, newer implementations of signal() don't reset the
  609.       handler to SIG_DFL when the handler function is called.  To get
  610.       around this problem, on systems that do not have wait3() but do
  611.       have SIGCLD, you need to reset the signal handler with a call to
  612.       signal() after doing at least one wait() within the handler, each
  613.       time it is called.
  614.  
  615.       The summary of all this is that on systems that have wait3(), you
  616.       should use that and your signal handler should loop, and on
  617.       systems that don't, you should have one call to wait() per
  618.       invocation of the signal handler.
  619.  
  620.       One more thing -- if you don't want to go through all of this
  621.       trouble, there is a portable way to avoid this problem, although
  622.       it is somewhat less efficient.  Your parent process should fork,
  623.       and then wait right there and then for the child process to
  624.       terminate.  The child process then forks again, giving you a
  625.       child and a grandchild.  The child exits immediately (and hence
  626.       the parent waiting for it notices its death and continues to
  627.       work), and the grandchild does whatever the child was originally
  628.       supposed to.  Since its parent died, it is inherited by init,
  629.       which will do whatever waiting is needed.  This method is
  630.       inefficient because it requires an extra fork, but is pretty much
  631.       completely portable.
  632.  
  633. 3.14) How do I get lines from a pipe as they are written instead of only in
  634.       larger blocks.
  635.  
  636.       From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  637.       Date: Sun, 16 Feb 92 20:59:28 -0500
  638.  
  639.       The stdio library does buffering differently depending on whether
  640.       it thinks it's running on a tty.  If it thinks it's on a tty, it
  641.       does buffering on a per-line basis; if not, it uses a larger
  642.       buffer than one line.
  643.  
  644.       If you have the source code to the client whose buffering you
  645.       want to disable, you can use setbuf() or setvbuf() to change the
  646.       buffering.
  647.  
  648.       If not, the best you can do is try to convince the program that
  649.       it's running on a tty by running it under a pty, e.g. by using
  650.       the "pty" program mentioned in question 3.9.
  651.  
  652. -- 
  653. Ted Timar - tmatimar@empress.com
  654. Empress Software, 3100 Steeles Ave E, Markham, Ont., Canada L3R 8T3
  655.